package com.imu.purse.modules.sms;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.imu.constant.Constant;
import com.imu.purse.modules.sms.config.ConfigUtil;
import com.imu.purse.modules.sms.entity.SmsLogEntity;
import com.imu.purse.modules.sms.exception.SMSException;
import com.imu.purse.modules.sms.service.SmsLogService;

@Component
public class SendSMS 
{
	public void send(SMSMessage message) 
	{
		if(null == message)
		{
			logger.info("参数为空.....");
			return;
		}
		String phone = message.getPhone();
		String code = message.getAuthcode();
		SMSMessage.Type type = message.getType();
		
		SmsLogEntity entity = null;
		Map<String, String> parameters = null;
		String json = null;
		
		try
		{
			entity = new SmsLogEntity();
			entity.setCode(code);
			entity.setPhone(phone);
			entity.setType(type.getType());
			entity.setTimeCreate(new Date());
			entity.setContent(String.format(template, code));
			
			if(null != active && !active.equals("pro"))
			{
				//保存到缓存
				cache.saveCode(type.getType(), phone, Constant.DEF_CODE);
				
				entity.setCode(Constant.DEF_CODE);
				entity.setState(Constant.SUC);
				//保存短信日志记录
				service.save(entity);
				return;
			}
			
			parameters = new HashMap<String, String>(4);
			parameters.put("Uid", name);
			parameters.put("Key", secret);
			parameters.put("smsMob", phone);
			parameters.put("smsText", String.format(template, code));
			
			json = BaseApi.get(host, parameters);
			
			if (json != null && Constant.SUC == Integer.parseInt(json))
			{
				entity.setState(Constant.SUC);
				//保存到缓存
				cache.saveCode(type.getType(), phone, code);
			} 
			else
			{
				entity.setState(Constant.FAI);
				entity.setDesc(Constant.FAIL);
				logger.error("SMS send failed");
			}
		}
		catch (SMSException e)
		{
			entity.setState(Constant.FAI);
			entity.setDesc(e.getMessage());
			logger.error("SMS send failed:" + e.toString());
		}
		
		if(null != entity) service.save(entity); //保存短信日志记录
	}
	
	public void send(String type,String...phones){
		if(phones == null || phones.length == 0){
			logger.info("phones is empty");
			return;
		}
		SmsLogEntity entity = null;
		Map<String,String> parameters = new HashMap<String, String>(4);
		parameters.put("Uid", name);
		parameters.put("Key", secret);
		parameters.put("smsText", String.format(bitTemplate));
		for(String phone : phones){
			try{
				entity = new SmsLogEntity();
				entity.setPhone(phone);
				entity.setType(type);
				entity.setTimeCreate(new Date());
				entity.setContent(String.format(bitTemplate));
				entity.setState(Constant.SUC);
				//推送短信
				parameters.put("smsMob", phone);
				BaseApi.get(host, parameters);
			}catch (SMSException e){
				entity.setState(Constant.FAI);
				entity.setDesc(e.getMessage());
				logger.error("SMS send failed:" + e.toString());
			}
			//保存短信日志记录
			service.save(entity);
		}
	}
	//key
    private final static String host = ConfigUtil.getValue("url.host", "http://utf8.api.smschinese.cn");
    
    //密钥
    private final static String secret = ConfigUtil.getValue("x-ca-secret", "9fb11d3d9bcc64641004");
    
    //密钥
    private final static String name = ConfigUtil.getValue("product.name", "币资讯短信");
    
    //模板
    private final static String template = ConfigUtil.getValue("product.template", "您的短信验证码是:%s,有效期为30分钟,请尽快使用,每天最多可发送10条。");

	// 日志记录
	private static final Logger logger = LoggerFactory.getLogger(SendSMS.class);
	//imu应用异常模板
	private static final String bitTemplate = ConfigUtil.getValue("bitcoin.error.template","IMU应用程序可能异常，请及时查看!");
    @Autowired
    private SMSCache cache;
	
	@Autowired
    private SmsLogService service;
	
	@Value("${spring.profiles.active}")
    private String active;
	
}
